package cn.cstech.aijia.ui.base;

/**
 * Created with Android Studio.
 * Title:BaseActivity
 * Description:
 * Copyright:Copyright (c) 2014
 * Company:中国平安健康保险有限公司
 * Author:Alice
 * Date:15/8/15
 * Time:下午9:28
 * Version 1.0
 */


import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.listener.PauseOnScrollListener;

import cn.cstech.aijia.R;
import cn.cstech.aijia.common.ErrorCode;
import cn.cstech.aijia.ui.base.title.BaseContentView;
import cn.cstech.aijia.ui.base.title.IActionTitleBar;
import cn.cstech.aijia.ui.base.utils.DialogUtil;
import cn.cstech.aijia.ui.base.utils.NoLeakHandler;
import cn.cstech.aijia.BaseApplication;
import cn.cstech.aijia.crash.UncaughtExceptionHandler;
import cn.cstech.aijia.ui.base.thirdpart.pulltorefresh.PullToRefreshGridView;
import cn.cstech.aijia.ui.base.thirdpart.pulltorefresh.PullToRefreshListView;
import cn.cstech.aijia.ui.base.title.ITitleBar;
import cn.cstech.aijia.ui.base.utils.LoadingDialog;
import cn.cstech.aijia.ui.base.utils.LocalUtils;

public class BaseActivity extends Activity implements ITitleBar,
        NoLeakHandler.HandlerCallback {

    protected NoLeakHandler mUIHandler;
    protected Context mContext;

    protected LoadingDialog mLoadingDialog;

    protected ListView listView;
    protected PullToRefreshGridView plgridView;
    protected PullToRefreshListView plistview;
    protected boolean addTitleBar = true;
    private BaseContentView mBaseContentView;

    @Override
    public void handleMessage(Message msg) {

    }

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        mContext = this;
        mUIHandler = new NoLeakHandler(this);

        Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler(getApplicationContext()));

        ((BaseApplication)getApplication()).addActivity(this);
    }

    @Override
    public void setContentView(int layoutResID) {
        if (addTitleBar) {
            super.setContentView(R.layout.sm_main_layout);
        } else {
            super.setContentView(R.layout.sm_main_no_title_layout);
        }

        // add main view to root view
        LayoutInflater inflater = LayoutInflater.from(this);
        mBaseContentView = (BaseContentView) findViewById(R.id.sm_main_root_view);
        View view = inflater.inflate(layoutResID, null);
        mBaseContentView.setMainContentView(view);
        initTitleBar();
    }

    public void showLoadingView(String msg) {
        if (mLoadingDialog == null) {
            mLoadingDialog = DialogUtil.showLoadingDialog(mContext, msg, true);
        }

        mLoadingDialog.setDlgMessage(msg);

        if (!mLoadingDialog.isShowing() && !isFinishing()) {
            mLoadingDialog.show();
        }
    }

    public void hideLoadingView() {
        if (mLoadingDialog != null && mLoadingDialog.isShowing() && !isFinishing()) {
            mLoadingDialog.dismiss();
        }
    }

    public void showMoreView(int iconResId, View.OnClickListener lsn) {
        if( mBaseContentView == null){
            return;
        }
        mBaseContentView.setRightImageView(iconResId,lsn);
    }

    public void setRightText(String text,View.OnClickListener lsn,int textColor){
        if( mBaseContentView == null){
            return;
        }
        mBaseContentView.setRightButton(text,lsn);
    }

    @Override
    protected void onResume() {
        super.onResume();

        applyScrollListener();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
    }

    private void applyScrollListener() {
        ImageLoader imageLoader = ImageLoader.getInstance();
        if (listView != null) {
            listView.setOnScrollListener(new PauseOnScrollListener(imageLoader,
                    true, true, getOnScrollListener()));
        }
        if (plgridView != null) {
            plgridView.setOnScrollListener(new PauseOnScrollListener(
                    imageLoader, true, true));
        }
        if (plistview != null) {
            plistview.setOnScrollListener(new PauseOnScrollListener(
                    imageLoader, true, true));
        }
    }

    protected AbsListView.OnScrollListener getOnScrollListener() {
        return null;
    }

    private void initTitleBar() {
        Log.d("wyg", "initTitleBar------------->>>");
        onTitleBarCreate();
    }

    protected void onTitleBarCreate() {

//        setDefaultBackAction(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                finish();
//            }
//        });
    }

    private void setDefaultBackAction(View.OnClickListener l) {
        if (mBaseContentView != null) {
            mBaseContentView.setDefaultBackActoin(l);
        }
    }

    public void showBackView(){
        setDefaultBackAction(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void onDestroy() {
        if (mUIHandler != null) {
            mUIHandler.setValid(true);
        }

        ((BaseApplication)getApplication()).removeActivity(this);

        super.onDestroy();
    }

    /**
     * @Description 显示错误页面
     * @author xiezhidong@pajk.cn
     * @param errorCode
     *            参考 ErrorCode
     * @param listener
     */

    public void showErrorPage(int errorCode, View.OnClickListener listener) {
        switch (errorCode) {
            case ErrorCode.STATUS_IO_EXCEPTION:
                showHttpDataError(true, listener);
                break;
            case ErrorCode.STATUS_NETWORK_EXCEPTION:
                showNetWorkError(1, listener);
                break;
            case ErrorCode.CONNECTTION_TIME_OUT:
                showNetWorkError(1, listener);
                break;
            case ErrorCode.DEVICE_TOKEN_MISSING:
                LocalUtils.showToast(this, "DEVICE_TOKEN_MISSING");
                break;

            case ErrorCode.NETWORK_UNAVAILABLE:
                showNetWorkError(0, listener);
                break;

            case ErrorCode.NOT_LOGIN:
                LocalUtils.showToast(this, "NOT_LOGIN");
                break;

            case ErrorCode.NO_RECOMMAND_DOCTORS:
                LocalUtils.showToast(this, "NO_RECOMMAND_DOCTORS");
                break;

            case ErrorCode.NO_GET_DOCTOR_INFO:
                LocalUtils.showToast(this, "NO_GET_DOCTOR_INFO");
                break;
            default:
                break;
        }
    }

    /**
     * @Description
     * @author xiezhidong@pajk.cn
     * @param errorCode
     *            -1 为隐藏所有
     */

    public void hideErrorPage(int errorCode) {
        switch (errorCode) {
            case ErrorCode.STATUS_IO_EXCEPTION:
                showHttpDataError(false);
                break;
            case ErrorCode.STATUS_NETWORK_EXCEPTION:
                hideNetWorkError();
                break;
            case ErrorCode.CONNECTTION_TIME_OUT:
                hideNetWorkError();
                break;
            case ErrorCode.DEVICE_TOKEN_MISSING:
                break;

            case ErrorCode.NETWORK_UNAVAILABLE:
                hideNetWorkError();
                break;

            case ErrorCode.NOT_LOGIN:
                break;

            case ErrorCode.NO_RECOMMAND_DOCTORS:
                break;

            case ErrorCode.NO_GET_DOCTOR_INFO:
                break;

            default:
                hideNetWorkError();
                showHttpDataError(false);
                break;
        }
    }

    @Override
    public void setTitleText(String title) {
        if (mBaseContentView != null) {
            mBaseContentView.setTitleText(title);
        }
    }

    @Override
    public void setTitleText(int title) {
        if (mBaseContentView != null) {
            mBaseContentView.setTitleText(title);
        }
    }

    @Override
    public void setLeftButton(String btnText, View.OnClickListener lsn) {
        if (mBaseContentView != null) {
            mBaseContentView.setLeftButton(btnText, lsn);
        }
    }

    @Override
    public void setLeftButton(int btnText, View.OnClickListener lsn) {
        if (mBaseContentView != null) {
            mBaseContentView.setLeftButton(btnText, lsn);
        }
    }

    @Override
    public void setLeftImageView(int imageRes, View.OnClickListener lsn) {
        if (mBaseContentView != null) {
            mBaseContentView.setLeftImageView(imageRes, lsn);
        }
    }

    @Override
    public void setRightButton(String btnText, View.OnClickListener lsn) {
        if (mBaseContentView != null) {
            mBaseContentView.setRightButton(btnText, lsn);
        }
    }

    @Override
    public void hideRightButton() {
        if (mBaseContentView != null) {
            mBaseContentView.hideRightButton();
        }
    }

    @Override
    public void setRightButton(int btnText, View.OnClickListener lsn) {
        if (mBaseContentView != null) {
            mBaseContentView.setRightButton(btnText, lsn);
        }
    }

    @Override
    public void setRightImageView(int imageRes, View.OnClickListener lsn) {
        if (mBaseContentView != null) {
            mBaseContentView.setRightImageView(imageRes, lsn);
        }
    }

    @Override
    public void setBackOnClick(View.OnClickListener lsn) {
        setDefaultBackAction(lsn);
    }

    @Override
    public void showTitleBar(boolean show) {
        if (mBaseContentView != null) {
            mBaseContentView.showTitleBar(show);
        }
    }

    @Override
    public void showNullPage(boolean show) {
        if (mBaseContentView != null) {
            if (show) {
                mBaseContentView.showErrorView(IActionTitleBar.ErrorType.EMPTYVIEW);
            } else {
                mBaseContentView.hideErrorView();
            }
        }
    }

    @Override
    public void showHttpDataError(boolean show) {
        if (mBaseContentView != null) {
            if (show) {
                mBaseContentView.showErrorView(IActionTitleBar.ErrorType.ERRORNET);
            } else {
                mBaseContentView.hideErrorView();
            }
        }
    }

    @Override
    public void showHttpDataError(boolean show, View.OnClickListener clickListener) {
        if (mBaseContentView != null) {
            if (show) {
                mBaseContentView.showErrorView(IActionTitleBar.ErrorType.ERRORNET);
            } else {
                mBaseContentView.hideErrorView();
            }
        }
    }

    @Override
    public void showNetWorkErrorOnTop(boolean show) {
        if (mBaseContentView != null) {
            if (show) {
                mBaseContentView.showErrorView(IActionTitleBar.ErrorType.ERRORTOP);
            } else {
                mBaseContentView.hideErrorView();
            }
        }
    }

    public void showErrorView(IActionTitleBar.ErrorType type) {
        if (mBaseContentView != null) {
            mBaseContentView.showErrorView(type);
        }
    }

    public void showErrorView(IActionTitleBar.ErrorType type, String alertText,
                              View.OnClickListener lsn) {
        if (mBaseContentView != null) {
            mBaseContentView.showErrorView(type, alertText, lsn);
        }
    }

    public void showErrorView(IActionTitleBar.ErrorType type, int iconRes, String alertText,
                              View.OnClickListener lsn) {
        if (mBaseContentView != null) {
            mBaseContentView.showErrorView(type, iconRes, alertText, lsn);
        }
    }

    @Override
    public void showNetWorkError(int i, View.OnClickListener listener) {
        // if (mBaseContentView != null) {
        // mBaseContentView.showNetWorkError(i, listener);
        // }
    }

    @Override
    public void hideNetWorkError() {
        if (mBaseContentView != null) {
            mBaseContentView.hideErrorView();
        }
    }

    public void showToast(String toastTxt) {
        Toast.makeText(this, toastTxt, Toast.LENGTH_SHORT).show();
    }

    public void showToast(int txtRes) {
        Toast.makeText(this, txtRes, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showCloseButton(boolean show, View.OnClickListener lsn) {
        if (mBaseContentView != null) {
            mBaseContentView.showCloseButton(show, lsn);
        }
    }

}

